package com.pmcc.cj.service;

import com.pmcc.cj.entity.*;
import com.pmcc.cj.entity.vo.OrderDetailVo;
import com.pmcc.cj.entity.vo.OrderVo;
import com.pmcc.cj.repository.*;
import com.pmcc.cj.vo.PingTaiVo;
import com.pmcc.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by tomas on 2020/10/13.
 */
@Transactional
@Service
public class CjOrderIndexService {

    @Autowired
    CjOrderIndexRepository cjOrderIndexRepository;
    @Autowired
    CjOrderRepository cjOrderRepository;
    @Autowired
    CjOrderDetailRepository cjOrderDetailRepository;
    @Autowired
    CjCommodityYunRepository yunRepository;
    @Autowired
    @Qualifier("primaryJdbcTemplate")
    private JdbcTemplate jdbcTemplate;
    @Autowired
    CjTaskRepository cjTaskRepository;
    @Autowired
    CjTaskService cjTaskService;
    @Autowired
    CjCustomerRepository cjCustomerRepository;
    @Autowired
    CjMoneyRecordRepository cjMoneyRecordRepository;

    public ResultBean queryOrderIndexByPage(CjOrderIndex model, AntdPage antdPage){
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        try {
            Page page=cjOrderIndexRepository.findAll(this.getSpecification(model),antdPage.getPageRequest());
            if(page != null && page.getContent().size()>0){
                for(CjOrderIndex orderIndex:(List<CjOrderIndex>) page.getContent()){
//                    List<CjOrder> list=cjOrderRepository.findByOrderIndexId(orderIndex.getId());
                    String sql="select * from cj_order where order_index_id='" + orderIndex.getId()+"' ";
                    List<CjOrder> list=jdbcTemplate.query(sql,new BeanPropertyRowMapper(CjOrder.class));
                    List<OrderVo> lt=new ArrayList<>();
                    if(list != null && list.size()>0){
                        for(CjOrder order: list){
                            OrderVo orderVo =new OrderVo();
                            orderVo.setId(order.getId());
                            orderVo.setGenre(order.getGenre()+"");
                            orderVo.setTotalmoney(order.getMoney().toString());
                            orderVo.setIsSaleRule(order.getIsSaleRule()+"");
                            orderVo.setIsNormal(order.getIsNormal()+"");
                            orderVo.setYunNum(order.getCommodityCount()+"");
                            orderVo.setYun(order.getYunId());
                            orderVo.setShopName(order.getYunName());
                            orderVo.setYunPrice(order.getPrice().toString());
                            sql="select * from cj_commodity_yun where id = '" + order.getYunId() +"'";
                            List<CjCommodityYun> yun=jdbcTemplate.query(sql,new BeanPropertyRowMapper(CjCommodityYun.class));
                            orderVo.setImageUrl(yun.get(0).getAddress());
                            sql ="select * from cj_order_detail where order_id='" + order.getId() +"'";
                            List<CjOrderDetail> lt1=jdbcTemplate.query(sql,new BeanPropertyRowMapper(CjOrderDetail.class));
                            List<OrderDetailVo> lt2=new ArrayList<>();
                            if(lt1 != null && lt1.size()>0){
                                for(CjOrderDetail orderDetail: lt1){
                                    OrderDetailVo orderDetailVo = new OrderDetailVo();
                                    orderDetailVo.setId(orderDetail.getId());
                                    orderDetailVo.setDose(orderDetail.getNumber()+"");
                                    orderDetailVo.setName(orderDetail.getName());
                                    lt2.add(orderDetailVo);
                                }
                            }
                            orderVo.setOrderDetailVos(lt2);
                            lt.add(orderVo);
                        }
                    }
                    orderIndex.setOrderVos(lt);
                }
            }
            resultBean.setPage(antdPage.getDefaultCurrent());
            resultBean.setTotal(page.getTotalElements());
            resultBean.setPageSize(antdPage.getPageSize());
            resultBean.setRows(page.getContent());
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }

    public Specification<CjOrderIndex> getSpecification(CjOrderIndex model) {
        return new Specification<CjOrderIndex>() {
            @Override
            public Predicate toPredicate(Root<CjOrderIndex> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
                List<Predicate> list = new ArrayList<Predicate>();
                if(model.getPayStatus()!= null){
                    list.add(builder.equal(root.get("payStatus").as(Integer.class),  model.getPayStatus()));
                }
                if(StringUtil.isNotEmpty(model.getEmployeeId())){
                    list.add(builder.equal(root.get("employeeId").as(String.class),  model.getEmployeeId()));
                }else {
                    //如果没传则查询当前登录人的
                    list.add(builder.equal(root.get("employeeId").as(String.class), OnlineUser.getEmployeeId()));
                }
                query.orderBy(builder.desc(root.get("createTime").as(Date.class)));
                Predicate[] p = new Predicate[list.size()];
                return builder.and(list.toArray(p));
            }
        };
    }



//    public ResultBean placeOnOrder(String id){
//        ResultBean resultBean = new ResultBean(ResultBean.FAIL);
//        try {
//            CjOrderIndex orderIndex = cjOrderIndexRepository.findOne(id);
//            List<CjOrder> list = cjOrderRepository.findByOrderIndexId(id);
//
//        }catch (Exception e){
//        }
//        return resultBean;
//    }

    public ResultBean pingTai(){
        ResultBean resultBean =new ResultBean(ResultBean.SUCCESS);
        try {
            List<PingTaiVo> list = new ArrayList<>();
            CjCustomer customer1= cjCustomerRepository.findOne("2c978b23752a450d01752aff74c8954g");
            PingTaiVo pingTaiVo1 = new PingTaiVo();
            pingTaiVo1.setName("平台");
            pingTaiVo1.setMoney(customer1.getMoney());
            List<CjMoneyRecord> lt1=cjMoneyRecordRepository.findByCustomerBelongIdOrderByCreateDateDesc("2c978b23752a450d01752aff74c8954g");
            if(lt1 != null && lt1.size()>0){
                for (CjMoneyRecord moneyRecord: lt1){
                    CjCustomer cjCustomer1 = cjCustomerRepository.findOne(moneyRecord.getCustomerIncomeId());
                    CjCustomer cjCustomer2 = cjCustomerRepository.findOne(moneyRecord.getCustomerPayId());
                    moneyRecord.setIncomeName(cjCustomer1.getCardName());
                    moneyRecord.setPayName(cjCustomer2.getCardName());
                }
            }
            pingTaiVo1.setMoneyRecords(lt1);
            list.add(pingTaiVo1);
            CjCustomer customer2= cjCustomerRepository.findOne("2c978b23752a450d01752aff74c7624k");
            PingTaiVo pingTaiVo2 = new PingTaiVo();
            pingTaiVo2.setName("厂家");
            pingTaiVo2.setMoney(customer2.getMoney());
            List<CjMoneyRecord> lt2=cjMoneyRecordRepository.findByCustomerBelongIdOrderByCreateDateDesc("2c978b23752a450d01752aff74c7624k");
            if(lt2 != null && lt2.size()>0){
                for (CjMoneyRecord moneyRecord: lt2){
                    CjCustomer cjCustomer1 = cjCustomerRepository.findOne(moneyRecord.getCustomerIncomeId());
                    CjCustomer cjCustomer2 = cjCustomerRepository.findOne(moneyRecord.getCustomerPayId());
                    moneyRecord.setIncomeName(cjCustomer1.getCardName());
                    moneyRecord.setPayName(cjCustomer2.getCardName());
                }
            }
            pingTaiVo2.setMoneyRecords(lt2);
            list.add(pingTaiVo2);
            resultBean.setRows(list);
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }
}
